package com.knife.blog.logic.impl;

import com.knife.web.tools.DbPageList;
import com.knife.web.tools.IPageList;
import com.knife.blog.domain.BlogInfo;
import com.knife.blog.domain.BlogNewsDoc;
import com.knife.blog.domain.BlogReview;
import com.knife.blog.domain.Photo;
import com.knife.blog.logic.BlogNewsDocService;
import com.knife.blog.logic.BlogReviewService;
import com.knife.blog.logic.CurrentUser;
import com.knife.blog.logic.HTMLGenerater;
import com.knife.blog.logic.PhotoService;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

public class BlogReviewServiceImpl extends DAOSupportService
        implements BlogReviewService {

    private BlogNewsDocService blogDocService = BlogCoreServiceImpl.getInstance();
    private PhotoService blogPhotoService = PhotoServiceImpl.getInstance();
    private static BlogReviewServiceImpl singleton = new BlogReviewServiceImpl();

    public static BlogReviewServiceImpl getInstance() {
        return singleton;
    }

    public int countUnCheckMessage(String username) {
        int ret = 0;
        String tmpSql = "select count(*) from blogreview where belonguser=? and status=0";
        java.util.Collection<Object> paras = new java.util.ArrayList<Object>();
        paras.add(username);
        Number num = (Number) this.dao.uniqueResult(tmpSql, paras);
        ret = Integer.parseInt(num.toString());
        return ret;
    }

    public BlogReview getBlogReview(String cid) {
        return ((BlogReview) this.dao.get(BlogReview.class, cid));
    }

    public Number getReviewNum(String parentId) {
        String scope = "parentid=? ";
        Collection<Object> paras = new ArrayList<Object>();
        paras.add(parentId);
        return ((Number) this.dao.uniqueResult("select count(*) from blogreview where " + scope, paras));
    }

    public boolean addBlogReview(BlogReview review) {
        boolean ret = false;
        BlogNewsDoc doc = this.blogDocService.getBlogNewsDoc(review.getParentid());
        review.setBelonguser(doc.getInputuser());
        review.setUrl("blog");
        review.setInputtime(new Date());
        review.setStatus(0);
        //if (CurrentUser.getActiveUser() != null) {
        //    review.setInputuser(CurrentUser.getActiveUser().getUserName());
        //}
        ret = this.dao.save(review);
        if (ret) {
            doc.setReplynum(new Integer(1));
            this.blogDocService.updateBlogNewsDoc(doc);
            BlogInfo blog = BlogUserServiceImpl.getInstance().getUserBlog(
                    doc.getInputuser());
            HTMLGenerater.saveLeftReviewAllToHTML(blog);
            HTMLGenerater.saveLeftReviewArticalToHTML(blog);
        }
        return ret;
    }

    public boolean addPhotoReview(BlogReview review) {
        boolean ret = false;
        Photo photo = PhotoServiceImpl.getInstance().getPhoto(
                review.getParentid());

        review.setBelonguser(photo.getInputuser());
        review.setUrl("photo");
        review.setInputtime(new Date());
        review.setStatus(0);
        //if (CurrentUser.getActiveUser() != null) {
        //    review.setInputuser(CurrentUser.getActiveUser().getUserName());
        //}
        ret = this.dao.save(review);
        if (ret) {
            photo.setReplynum(new Integer(1));
            PhotoServiceImpl.getInstance().updatePhoto(photo);
            BlogInfo blog = BlogUserServiceImpl.getInstance().getUserBlog(
                    photo.getInputuser());
            HTMLGenerater.saveLeftReviewAllToHTML(blog);

            HTMLGenerater.saveLeftReviewPhotoToHTML(blog);
        }
        return ret;
    }

    public int batchDelBlogReview(String[] cids) {
        int ret = 0;
        for (int i = 0; i < cids.length; ++i) {
            if (delBlogReview(cids[i])) {
                ++ret;
            }
        }
        return ret;
    }

    public boolean delBlogReview(String cid) {
        boolean ret = false;
        BlogReview review = getBlogReview(cid);
        ret = this.dao.del(review);
        BlogNewsDoc doc = this.blogDocService.getBlogNewsDoc(review.getParentid());
        Photo photo = this.blogPhotoService.getPhoto(review.getParentid());
        if (ret) {
            doc.setReplynum(doc.getReplynum()-1);
            blogDocService.updateBlogNewsDoc(doc);
            BlogInfo blog = BlogUserServiceImpl.getInstance().getUserBlog(
                    doc.getInputuser());
            HTMLGenerater.saveLeftReviewAllToHTML(blog);
            if (review.getUrl().equals("blog")) {
                HTMLGenerater.saveLeftReviewArticalToHTML(blog);
                HTMLGenerater.saveToHTML(doc);
            } else {
                HTMLGenerater.saveLeftReviewPhotoToHTML(blog);
                HTMLGenerater.savePhotoDocToHtml(photo);
            }
        }

        return ret;
    }

    public boolean delBlogReview(BlogNewsDoc doc) {
        boolean ret = false;
        IPageList reviewofDoc = queryBlogReview(doc.getCid(), 1,
                2147483647);
        List list = reviewofDoc.getResult();
        if (list == null) {
            return true;
        }
        for (int i = 0; i < list.size(); ++i) {
            BlogReview review = (BlogReview) list.get(i);
            ret = this.dao.del(review);
        }
        BlogInfo blog = BlogUserServiceImpl.getInstance().getUserBlog(doc.getInputuser());
        HTMLGenerater.saveLeftReviewAllToHTML(blog);
        HTMLGenerater.saveLeftReviewArticalToHTML(blog);
        HTMLGenerater.saveLeftReviewPhotoToHTML(blog);
        return ret;
    }

    public boolean updateBlogReview(BlogReview review) {
        boolean ret = false;
        ret = this.dao.update(review);
        BlogNewsDoc doc = this.blogDocService.getBlogNewsDoc(review.getParentid());
        BlogInfo blog = BlogUserServiceImpl.getInstance().getUserBlog(
                doc.getInputuser());
        HTMLGenerater.saveLeftReviewAllToHTML(blog);
        if (review.getUrl().equals("blog")) {
            HTMLGenerater.saveLeftReviewArticalToHTML(blog);
            HTMLGenerater.saveToHTML(blog);
        } else {
            HTMLGenerater.saveLeftReviewPhotoToHTML(blog);
            HTMLGenerater.savePhotoToHTMLByBlog(blog);
        }
        return ret;
    }

    public boolean check(String cid) {
        boolean ret = false;
        BlogReview blogreview = getBlogReview(cid);
        blogreview.setStatus(1);
        ret = this.dao.update(blogreview);
        if(ret){
            BlogNewsDoc doc = this.blogDocService.getBlogNewsDoc(blogreview.getParentid());
            Photo photo = this.blogPhotoService.getPhoto(blogreview.getParentid());
            BlogInfo blog = BlogUserServiceImpl.getInstance().getUserBlog(
                    doc.getInputuser());
            if (blogreview.getUrl().equals("blog")) {
                doc.setReplynum(doc.getReplynum()+1);
                ret = blogDocService.updateBlogNewsDoc(doc);
                HTMLGenerater.saveLeftReviewArticalToHTML(blog);
                HTMLGenerater.saveToHTML(doc);
            } else {
                photo.setReplynum(photo.getReplynum()+1);
                ret = blogPhotoService.updatePhoto(photo);
                HTMLGenerater.saveLeftReviewPhotoToHTML(blog);
                HTMLGenerater.savePhotoDocToHtml(photo);
            }
            HTMLGenerater.saveToHTML(blog);
        }
        return ret;
    }

    public IPageList queryBlogReview(String parentId, int currentPage, int pageSize) {
        String scope = "parentid=? and status>0 order by inputtime desc";
        Collection<Object> paras = new ArrayList<Object>();
        paras.add(parentId);
        DbPageList pList = new DbPageList(BlogReview.class, scope, paras);
        pList.doList(currentPage, pageSize);
        return pList;
    }

    public List getTopReviews(String username, int topNum) {
        String scope = "belonguser=? and status>0 order by inputtime desc";
        Collection<Object> paras = new ArrayList<Object>();
        paras.add(username);
        DbPageList pList = new DbPageList(BlogReview.class, scope, paras);
        pList.doList(1, topNum);
        return pList.getResult();
    }

    public List getTopBlogReviews(String username, int topNum) {
        String scope = "belonguser=? and url='blog' and status>0 order by inputtime desc";
        Collection<Object> paras = new ArrayList<Object>();
        paras.add(username);
        DbPageList pList = new DbPageList(BlogReview.class, scope, paras);
        pList.doList(1, topNum);
        return pList.getResult();
    }

    public List getTopPhotoReviews(String username, int topNum) {
        String scope = "belonguser=? and url='photo' and status>0 order by inputtime desc";
        Collection<Object> paras = new ArrayList<Object>();
        paras.add(username);
        DbPageList pList = new DbPageList(BlogReview.class, scope, paras);
        pList.doList(1, topNum);
        return pList.getResult();
    }

    public IPageList queryUserBlogReviews(String username, int currentPage, int pageSize) {
        String scope = "belonguser=? and status>0 order by inputtime desc";
        Collection<Object> paras = new ArrayList<Object>();
        paras.add(username);
        DbPageList pList = new DbPageList(BlogReview.class, scope, paras);
        pList.doList(currentPage, pageSize);
        return pList;
    }

    public IPageList queryUserBlogReviewsAll(String orderBy, int currentPage, int pageSize) {
        String scope = "1=1 and status>0 order by " + orderBy;
        DbPageList pList = new DbPageList(BlogReview.class, scope);
        pList.doList(currentPage, pageSize);
        return pList;
    }

    public IPageList queryUserBlogUnReview(String orderBy, int currentPage, int pageSize) {
        String scope = "1=1 and status=0 order by " + orderBy;
        DbPageList pList = new DbPageList(BlogReview.class, scope);
        pList.doList(currentPage, pageSize);
        return pList;
    }

    public IPageList queryUserPhotoReviews(String username, int currentPage, int pageSize) {
        String scope = "parentid=? and status>0 order by inputtime desc";
        Collection<Object> paras = new ArrayList<Object>();
        paras.add(username);
        DbPageList pList = new DbPageList(BlogReview.class, scope, paras);
        pList.doList(currentPage, pageSize);
        return pList;
    }
}
